set :default_stage, "development"
set :stages, %w(production development staging)
require 'capistrano/ext/multistage'

set :application, "com.cardbook.cake-tweet"
set :repository,  "http://vm.cardbook.com/svn/web/cake-tweet/trunk"

# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
# set :deploy_via, :export

# set :copy_exclude, [".git/*", ".gitignore"]
set :copy_exclude, [".svn/*"]
set :copy_compression, :gzip

# If you aren't using Subversion to manage your source code, specify
# your SCM below:
# set :scm, :subversion
set :scm, :subversion
set :svn_username, "yoophi"
set :svn_password, "yoophi"

# role :web, "your web-server here"
# role :db,  "your db-server here", :primary => true

# server '192.168.88.129', :app, :primary => true # set :user, 'yoophi'

after 'deploy:setup', 'deploy:change_permission', 'cakephp:build_tmp_dir'
#after 'deploy:symlink', 'whoru:symlink', 'whoru:set_permission'

namespace :web do 
    desc "Lock the current access during deployment" 
	task :disable, :roles => :app do 
		run "touch #{current_release}/webroot/.capistrano-lock" 
	end 

	desc "Enable the current access after deployment" 
		task :enable, :roles => :app do 
		run "rm #{current_release}/webroot/.capistrano-lock" 
	end 
end 

namespace :deploy do
    desc "This is here to overide the original :restart"
    task :restart, :roles => :app do
        # do nothing.
    end

    desc "This is here to overide the original :finalize_update"
    task :finalize_update, :roles => :app do
        # Link cakephp. Not the ideal linking but it works.
        #run "#{try_sudo} ln -s #{shared_path}/cakephp-1.3.2/cake #{current_release}/cake"

        # Link configuration files
        run "ln -snf #{shared_path}/config/core.php #{current_release}/config/core.php"
        run "ln -snf #{shared_path}/config/database.php #{current_release}/config/database.php"
        
        # Link tmp
        run "#{try_sudo} rm -rf #{current_release}/tmp"
        run "#{try_sudo} ln -s #{shared_path}/tmp #{current_release}/tmp"
        run "#{try_sudo} chown www-data:www-data #{current_release}/tmp -R"

        # Link profile_img interest_img
        # run "ln -snf #{shared_path}/profile_img #{current_release}/webroot/images/profile_img"
        # run "ln -snf #{shared_path}/interest_img #{current_release}/webroot/images/interest_img"

    end

    desc "Change file permissions."
    task :change_permission, :except => { :no_release => true } do
        run "#{try_sudo} chown -R #{user}:#{user} #{deploy_to}"
    end
end

namespace :cakephp do
    desc "Blow up all the cache files CakePHP uses, ensuring a clean restart."
    task :clear_cache do
        run "#{try_sudo} rm -rf #{shared_path}/tmp/*"
        build_tmp_dir
    end

    desc "Build CakePHP tmp directory."
    task :build_tmp_dir do
        shared_tmp = File.join(shared_path, "tmp")
        tmp_children = %w(cache sessions logs tests)
        cache_children = %w(models persistent views)
        dirs = []
        dirs += tmp_children.map {|d| File.join(shared_tmp, d)}
        dirs += cache_children.map {|d| File.join(File.join(shared_tmp, 'cache'), d)}

        run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chown www-data:www-data #{shared_path}/tmp && #{try_sudo} chown -R www-data:www-data #{dirs.join(' ')}"
        run "#{try_sudo} chmod -R 777 #{shared_path}/tmp"
    end

    desc "Copy default setting files."
    task :copy_settings, :expect => { :no_release => true } do
        run "#{try_sudo} mkdir -p #{shared_path}/config"
        run "#{try_sudo} chown -R #{user}:#{user} #{shared_path}/config"

        begin
            %w(core.php database.php).each do |setting_file| 
                remote_file = File.join(shared_path, 'config', setting_file)
                upload("default/#{stage}/#{setting_file}", remote_file)
                puts "Upload success!"
            end
        rescue Exception
            puts 'error.'
        end
    end

    desc "Clear view cache"
    task :clear_view_cache, :expect => { :no_release => true } do
        run "#{try_sudo} rm -rf #{shared_path}/tmp/cache/views/*"
    end

    # desc "Install SVN."
    # task :install_svn, :expect => { :no_release => true } do
    #     sudo "apt-get install subversion --yes"
    # end

    desc "Verify CakePHP TestSuite pass"
    task :testsuite, :roles => :app do
        # run "#{release_path}/cake/console/cake testsuite app all -app #{release_path}", :env => { :TERM => "linux" } do |channel, stream, data| 
        run "#{current_release}/cake/console/cake testsuite app all -app #{current_release}", :env => { :TERM => "linux" } do |channel, stream, data| 
            if stream == :err then 
                error = CommandError.new("CakePHP TestSuite failed") 
                raise error 
            else 
                puts data 
            end 
        end 
    end
end
